<template>
  <div class="wallet-container">
    <!-- Header部分 -->
    <header class="wallet-header">
      <div class="header-content">
        <img :src="user.userImg" alt="用户头像" class="user-avatar">
        <div class="user-info">
          <h3 class="user-id">{{ user.userId }}</h3>
          <p class="user-name">{{ user.userName }}</p>
        </div>
      </div>
    </header>

    <!-- 钱包信息部分 -->
    <div class="wallet-info">
      <div class="credit-summary">
        <div class="credit-balance">
          <h2>我的积分</h2>
          <h1 class="balance-amount">{{creditSum}}</h1>
        </div>

        <div class="filter-buttons">
          <button
              :class="{active: status===0}"
              @click="getCreditTransaction(0)">
            全部记录
          </button>
          <button
              :class="{active: status===1}"
              @click="getCreditTransaction(1)">
            赚取记录
          </button>
          <button
              :class="{active: status===2}"
              @click="getCreditTransaction(2)">
            消费记录
          </button>
        </div>
      </div>

      <div class="transaction-list">
        <ul class="order">
          <li v-for="item in creditArr" :key="item.eventId" class="transaction-item">
            <div class="transaction-main" @click="detailetShow(item)">
              <div class="transaction-icon">
                <i :class="item.credit > 0 ? 'fa fa-plus-circle earn' : 'fa fa-minus-circle consume'"></i>
              </div>
              <div class="transaction-details">
                <h4 class="transaction-source">{{creditSource(item.channelId)}}</h4>
                <p class="transaction-date">{{formatDate(item.createTime)}}</p>
              </div>
              <div class="transaction-amount" :class="item.credit > 0 ? 'earn' : 'consume'">
                {{creditToken(item.credit)}}{{Math.abs(item.credit)}}
              </div>
              <i class="fa" :class="item.isShowDetailet ? 'fa-chevron-up' : 'fa-chevron-down'"></i>
            </div>
            <div v-show="item.isShowDetailet" class="transaction-detail">
              <p>交易ID: {{item.eventId}}</p>
            </div>
          </li>
        </ul>

        <div v-if="creditArr.length === 0" class="empty-state">
          <i class="fa fa-wallet"></i>
          <p>暂无积分记录</p>
        </div>
      </div>

      <div class="pagination">
        <button @click="pageChange(-1)" :disabled="pageNow === 1">
          <i class="fa fa-chevron-left"></i> 上一页
        </button>
        <span class="page-indicator">第 {{pageNow}} 页</span>
        <button @click="pageChange(1)" :disabled="creditArr.length < 8">
          下一页 <i class="fa fa-chevron-right"></i>
        </button>
      </div>
    </div>

    <!-- 底部菜单部分 -->
    <Footer></Footer>
  </div>
</template>

<script>
import { ref, onMounted, getCurrentInstance } from 'vue';
import Footer from '../components/Footer.vue';
import axios from 'axios';
import qs from 'qs';

export default {
  name: 'Credit',
  setup() {
    const user = ref({});
    const creditSum = ref(0);
    const creditArr = ref([]);
    const pageNow = ref(1);
    const { proxy } = getCurrentInstance();
    const status = ref(0);

    const creditSource = (type) => {
      const sources = {
        0: "订单系统",
        1: "会员系统",
        2: "红包系统",
		3: "演示使用"
      };
      return sources[type] || "其他";
    };

    const creditToken = (credit) => {
      return credit > 0 ? "+" : "-";
    };

    const formatDate = (dateString) => {
      if (!dateString) return '';
      const date = new Date(dateString);
      return `${date.getFullYear()}-${(date.getMonth()+1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;
    };

    const getCreditTransaction = async (operation) => {
      status.value = operation;
      let url = "";
      switch(operation){
        case 0:
          url = `CreditController/listCreditTransactionByPage/${user.value.userId}/${pageNow.value}/8`;
          break;
        case 1:
          url = `CreditController/listCreditEarnTransactionByPage/${user.value.userId}/${pageNow.value}/8`;
          break;
        case 2:
          url = `CreditController/listCreditConsumeTransactionByPage/${user.value.userId}/${pageNow.value}/8`;
      }
      try {
        const response = await axios.get(url);
        let result = response.data;
        result.forEach(orders => {
          orders.isShowDetailet = false;
        });
        creditArr.value = result;
      } catch (error) {
        console.error(error);
      }
    };

    const pageChange = (isNext) => {
      if(pageNow.value === 1 && isNext === -1){
        proxy.$toast("已经是第一页");
        return;
      }
      if(creditArr.value.length < 8 && isNext === 1){
        proxy.$toast("没有更多内容了");
        return;
      }
      pageNow.value += isNext;
      getCreditTransaction(status.value);
    };

    const detailetShow = (orders) => {
      orders.isShowDetailet = !orders.isShowDetailet;
    };

    onMounted(() => {
      const getCreditSum = async () => {
        try {
          const response = await axios.get(
              `CreditController/getCreditById/${user.value.userId}`);
          creditSum.value = response.data;
        } catch (error) {
          console.error(error);
        }
      };
      user.value = proxy.$getSessionStorage('user');
      getCreditSum();
      getCreditTransaction(0);
    });

    return {
      user,
      creditSum,
      creditSource,
      creditToken,
      creditArr,
      detailetShow,
      getCreditTransaction,
      status,
      pageChange,
      pageNow,
      formatDate
    };
  },
  components: {
    Footer
  }
};
</script>

<style scoped>
.wallet-container {
  font-family: 'PingFang SC', 'Helvetica Neue', Arial, sans-serif;
  background-color: #f5f5f5;
  min-height: 100vh;
  padding-bottom: 5vw; /* 为底部导航栏留出空间 */
}

/* Header部分 */
.wallet-header {
  background: linear-gradient(135deg, #0097ff, #0066cc);
  color: white;
  padding: 20px 15px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.header-content {
  display: flex;
  align-items: center;
}

.user-avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.3);
  object-fit: cover;
}

.user-info {
  margin-left: 15px;
}

.user-id {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.user-name {
  margin: 5px 0 0;
  font-size: 14px;
  opacity: 0.9;
}

/* 积分概览 */
.credit-summary {
  background: white;
  border-radius: 12px;
  margin: 15px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.credit-balance {
  text-align: center;
  margin-bottom: 20px;
}

.credit-balance h2 {
  font-size: 16px;
  color: #666;
  margin-bottom: 5px;
}

.balance-amount {
  font-size: 36px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

/* 筛选按钮 */
.filter-buttons {
  display: flex;
  justify-content: space-between;
  margin-top: 15px;
}

.filter-buttons button {
  flex: 1;
  padding: 10px 0;
  margin: 0 5px;
  border-radius: 20px;
  background: #f0f0f0;
  color: #666;
  border: none;
  font-size: 14px;
  transition: all 0.3s ease;
}

.filter-buttons button.active {
  background: #0097ff;
  color: white;
  box-shadow: 0 2px 6px rgba(0, 151, 255, 0.3);
}

/* 交易列表 */
.transaction-list {
  background: white;
  border-radius: 12px;
  margin: 15px;
  padding: 0 15px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.transaction-item {
  border-bottom: 1px solid #f0f0f0;
  padding: 15px 0;
}

.transaction-item:last-child {
  border-bottom: none;
}

.transaction-main {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.transaction-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(0, 151, 255, 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
}

.transaction-icon .earn {
  color: #4CAF50;
  font-size: 20px;
}

.transaction-icon .consume {
  color: #F44336;
  font-size: 20px;
}

.transaction-details {
  flex: 1;
}

.transaction-source {
  margin: 0 0 5px 0;
  font-size: 16px;
  color: #333;
}

.transaction-date {
  margin: 0;
  font-size: 12px;
  color: #999;
}

.transaction-amount {
  font-weight: 600;
  font-size: 16px;
  margin-right: 10px;
}

.transaction-amount.earn {
  color: #4CAF50;
}

.transaction-amount.consume {
  color: #F44336;
}

.transaction-detail {
  padding: 10px 0 0 52px;
  font-size: 12px;
  color: #999;
  border-top: 1px dashed #eee;
  margin-top: 10px;
}

/* 空状态 */
.empty-state {
  text-align: center;
  padding: 40px 0;
  color: #ccc;
}

.empty-state i {
  font-size: 50px;
  margin-bottom: 15px;
}

.empty-state p {
  margin: 0;
  font-size: 14px;
}

/* 分页 */
.pagination {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px;
}

.pagination button {
  padding: 8px 15px;
  border-radius: 20px;
  background: white;
  border: 1px solid #ddd;
  color: #666;
  font-size: 14px;
  display: flex;
  align-items: center;
}

.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.pagination button i {
  margin: 0 5px;
  font-size: 12px;
}

.page-indicator {
  font-size: 14px;
  color: #666;
}

/* 底部菜单样式 - 保持不变 */
.footer {
  width: 100%;
  height: 5vw;
  border-top: solid 1px #DDD;
  background-color: #fff;
  position: fixed;
  left: 0;
  bottom: 0;
  display: flex;
  justify-content: space-around;
  align-items: center;
  list-style: none;
  margin: 0;
  padding: 0;
}
</style>